package com.ruoyi.historyVideo.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.historyVideo.domain.HistoryVideo;
import com.ruoyi.historyVideo.service.IHistoryVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * 历史视频Controller
 *
 * @author ruoyi
 * @date 2024-11-26
 */
@RestController
@RequestMapping("/historyVideo/historyVideo")
public class HistoryVideoController extends BaseController {
    @Autowired
    private IHistoryVideoService historyVideoService;

    /**
     * 查询历史视频列表
     */
    @PreAuthorize("@ss.hasPermi('historyVideo:historyVideo:list')")
    @GetMapping("/list")
    public TableDataInfo list(HistoryVideo historyVideo) {
        startPage();
        List<HistoryVideo> list = historyVideoService.selectHistoryVideoList(historyVideo);
        return getDataTable(list);
    }

    /**
     * 导出历史视频列表
     */
    @PreAuthorize("@ss.hasPermi('historyVideo:historyVideo:export')")
    @Log(title = "历史视频", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HistoryVideo historyVideo) {
        List<HistoryVideo> list = historyVideoService.selectHistoryVideoList(historyVideo);
        ExcelUtil<HistoryVideo> util = new ExcelUtil<HistoryVideo>(HistoryVideo.class);
        util.exportExcel(response, list, "历史视频数据");
    }

    /**
     * 获取历史视频详细信息
     */
    @PreAuthorize("@ss.hasPermi('historyVideo:historyVideo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(historyVideoService.selectHistoryVideoById(id));
    }

    /**
     * 新增历史视频
     */
    @PreAuthorize("@ss.hasPermi('historyVideo:historyVideo:add')")
    @Log(title = "历史视频", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HistoryVideo historyVideo) {
        return toAjax(historyVideoService.insertHistoryVideo(historyVideo));
    }

    /**
     * 修改历史视频
     */
    @PreAuthorize("@ss.hasPermi('historyVideo:historyVideo:edit')")
    @Log(title = "历史视频", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HistoryVideo historyVideo) {
        return toAjax(historyVideoService.updateHistoryVideo(historyVideo));
    }

    /**
     * 删除历史视频
     */
    @PreAuthorize("@ss.hasPermi('historyVideo:historyVideo:remove')")
    @Log(title = "历史视频", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(historyVideoService.deleteHistoryVideoByIds(ids));
    }

    @GetMapping("/one")
    public AjaxResult one(HistoryVideo historyVideo) {
        System.out.println(historyVideo);
        Date createTime = historyVideo.getCreateTime();
        System.out.println(createTime);
        LocalDate localDate = createTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

        System.out.println(localDate);

        List<HistoryVideo> historyVideoList = historyVideoService.list();

        for (HistoryVideo video : historyVideoList) {
            LocalDate videoDate = video.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            System.out.println(localDate);
            System.out.println(videoDate);
            if (localDate.equals(videoDate)) {
                return AjaxResult.success(video);
            }
        }
        System.out.println("renull");
        return null;
    }
}
